當image更新版本之後,可以利用RollingUpdate
替換pod裡container的image版本,進行不中斷服務的更新。
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-demo
labels:
app: ubuntu
spec:
replicas: 2
selector:
matchLabels:
app: ubuntu
template:
metadata:
labels:
app: ubuntu
spec:
containers:
- name: ubuntu
image: ubuntu:20.04
args: [bash, -c, 'for ((i = 0; ; i++)); do echo "$i: $(date)"; sleep 100; done']
部屬Deployment之後,再查看pod
root@master:/# kubectl apply -f deployment.yaml
deployment.apps/deployment-demo created
root@master:/# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-demo-68d5b9f46-wt5gr 1/1 Running 0 4s
deployment-demo-68d5b9f46-z8hbr 1/1 Running 0 4s
進入deployment-demo-68d5b9f46-wt5gr,並查看當前ubuntu版本
root@master:/# kubectl exec -ti deployment-demo-68d5b9f46-wt5gr -- bash
root@deployment-demo-68d5b9f46-wt5gr:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.4 LTS"
退出pod之後,更換image版本
root@master:/test# kubectl set image deployment/deployment-demo ubuntu=ubuntu:18.04 --record
deployment.apps/deployment-demo image updated
再次查看pod並進入到pod內查看ubuntu版本
root@master:/# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-demo-94fc48665-mvtz2 1/1 Running 0 68s
deployment-demo-94fc48665-xq8b9 1/1 Running 0 65s
root@master:/# kubectl exec -ti deployment-demo-94fc48665-mvtz2 -- bash
root@deployment-demo-94fc48665-mvtz2:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.6 LTS"
查看更新紀錄
root@master:/# kubectl rollout history deployment deployment-demo
deployment.apps/deployment-demo
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deployment/deployment-demo ubuntu=ubuntu:18.04 --record=true
當更新後發現系統有問題時,可以利用回滾功能把系統回復。你可以回到上一版本
或回到指定版本
回到上一版本
kubectl rollout undo deployment [name]
回到指定版本
kubectl rollout undo deployment [name] --to-revision=[version]
假如我要回到第一版的話:
root@master:/# kubectl rollout undo deployment deployment-demo --to-revision=1
deployment.apps/deployment-demo rolled back
root@master:/# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-demo-68d5b9f46-rwgzr 1/1 Running 0 12s
deployment-demo-68d5b9f46-skqcq 1/1 Running 0 9s
deployment-demo-94fc48665-mvtz2 1/1 Terminating 0 17m
deployment-demo-94fc48665-xq8b9 1/1 Terminating 0 17m
root@master:/# kubectl exec -ti deployment-demo-68d5b9f46-rwgzr -- bash
root@deployment-demo-68d5b9f46-rwgzr:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.4 LTS"